from .stock_data import StockDataService

class TradingStrategy:
    def __init__(self, stock):
        self.stock = stock
        self.profit_target = 0.05
        self.stop_loss = -0.02
        self.data_service = StockDataService()

    def calculate_signals(self):
        """计算买卖信号"""
        # 获取数据
        df = self.data_service.get_k_data(self.stock.code)
        if df is None or len(df) < 26:
            return None

        # 计算技术指标
        df = self.data_service.calculate_technical_indicators(df)

        # 生成买入信号
        df['buy_signal'] = False
        
        # MACD金叉：当日MACD柱由负变正
        macd_cross = (df['macd_hist'].shift(1) < 0) & (df['macd_hist'] > 0)
        
        # 成交量大于5日均线
        volume_condition = df['volume'] > df['volume_ma5']
        
        # RSI小于65
        rsi_condition = df['rsi'] < 65

        # 综合买入条件
        df.loc[macd_cross & volume_condition & rsi_condition, 'buy_signal'] = True

        return df 